/**
 *
 */
package anysware.source;

import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

/**
 * TODO: doc me
 *
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 111 $
 * @version
 */
@Entity
public class Source implements Serializable {
	/**
	 *
	 */
	private static final long	serialVersionUID	= 2491828189174146127L;
	@Column(updatable = false)
	private Date	          created	         = new Date();
	@Id
	@GeneratedValue
	private Long	          id;
	private Date	          lastChecked;
	private Date	          lastModified;
	@Column(unique = true, nullable = false)
	private String	          name;
	@Column(unique = true, nullable = false)
	private URL	              path;
	@Column(nullable = false)
	private String	          type;
	@Transient
	private URI	              uri;

	public Source() {
		super();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final Source other = (Source) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	/**
	 * @return the created
	 */
	public Date getCreated() {
		return created;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the lastChecked
	 */
	public Date getLastChecked() {
		return lastChecked;
	}

	/**
	 * @return the lastModified
	 */
	public Date getLastModified() {
		return lastModified;
	}

	/**
	 * @return the title
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the path
	 */
	public URL getPath() {
		return path;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @return the uri
	 */
	public URI getUri() {
		if (id == null) {
			return null;
		}
		if (uri == null) {
			try {
				return new URI(type + ":" + id);
			} catch (final URISyntaxException e) {
				throw new RuntimeException(e);
			}
		}
		return uri;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (name == null ? 0 : name.hashCode());
		return result;
	}

	/**
	 * @param created
	 *            the created to set
	 */
	public void setCreated(final Date created) {
		this.created = created;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(final Long id) {
		this.id = id;
	}

	/**
	 * @param lastChecked
	 *            the lastChecked to set
	 */
	public void setLastChecked(final Date lastChecked) {
		this.lastChecked = lastChecked;
	}

	/**
	 * @param lastModified
	 *            the lastModified to set
	 */
	public void setLastModified(final Date lastModified) {
		this.lastModified = lastModified;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setName(final String title) {
		name = title;
	}

	/**
	 * @param path
	 *            the path to set
	 */
	public void setPath(final URL path) {
		this.path = path;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(final String type) {
		this.type = type;
	}

	/**
	 * @param uri
	 *            the uri to set
	 */
	public void setUri(final URI uri) {
		this.uri = uri;
	}
}
